extends Node
#class_name DataManager

var data:Dictionary

func _init() -> void:
	load_data(["characters","skills","buffers"])
	print(data)

func load_data(_files_name:Array[String]):
	for file_name in _files_name:
		var path = "res://datas/" + file_name + ".csv"
		var temp_data:Dictionary = {}
		if FileAccess.file_exists(path):
			var file = FileAccess.open(path,FileAccess.READ)
			file.get_csv_line()
			var keys = file.get_csv_line()
			while file.get_position()<file.get_length():
				var items = file.get_csv_line()
				for index in items.size():
					if not temp_data.has(items[0]):
						temp_data[items[0]] = {}
					# 如果不是以param开头，就直接设置key，value，否则把param开头的列一起写到param数组中
					if not keys[index].begins_with("param"):
						if keys[index] == "buffers":
							if items[index] != "":
								temp_data[items[0]]["buffers"] = Utils.parse_string_to_dictionary(items[index])
						else:
							temp_data[items[0]].merge(set_value(keys[index],items[index]))
					else:
						if not temp_data[items[0]].has("params"):
							temp_data[items[0]].params = []
						if items[index] != "": 
							temp_data[items[0]].params.append(set_value(keys[index],items[index]))
			file.close()
		data[file_name] = temp_data

func set_value(_key:String,_value:String):
	var val = _value
	if _key.ends_with("_int"):
		_key = _key.replace("_int","")
		val = int(_value)
	elif _key.ends_with("_float"):
		_key = _key.replace("_float","")
		val = float(_value)
	if _key.begins_with("param"):
		return val
	else:
		return {_key:val}
